﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ListForms;

namespace ListsForm
{
    public partial class FrmRole : FrmBaseListForm
    {
        public FrmRole()
        {
            InitializeComponent();
        }

        public override void InitGridColumns()
        {
            DevExpress.XtraGrid.Columns.GridColumn colName = new DevExpress.XtraGrid.Columns.GridColumn();
            colName.Caption = "Name";
            colName.FieldName = "AccessAuthorityName";
            colName.Name = "colName";
            colName.Visible = true;
            colName.Width = 100;
            colName.MinWidth = 100;
            colName.OptionsColumn.FixedWidth = true;
            colName.VisibleIndex = 1;

            DevExpress.XtraGrid.Columns.GridColumn colDescription = new DevExpress.XtraGrid.Columns.GridColumn();
            colDescription.Caption = "Description";
            colDescription.FieldName = "AccessAuthorityDescription";
            colDescription.Name = "colDescription";
            colDescription.Visible = true;
            colDescription.Width = 100;
            colDescription.MinWidth = 100;
            colDescription.OptionsColumn.FixedWidth = true;
            colDescription.VisibleIndex = 2;

            this.gridViewDsDoiTuong.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] {
                colName, colDescription});

            foreach (DevExpress.XtraGrid.Columns.GridColumn gc in gridViewDsDoiTuong.Columns)
            {
                gc.FilterMode = DevExpress.XtraGrid.ColumnFilterMode.DisplayText;
                gc.OptionsFilter.AutoFilterCondition = DevExpress.XtraGrid.Columns.AutoFilterCondition.Contains;
            }
        }

        public override void LoadData()
        {
            try
            {
                DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, DB.AccessAuthorities);
                gridDSDoiTuong.DataSource = DB.AccessAuthorities.ToList();
            }
            catch (Exception ex)
            {
                BasicForms.FrmWarning.Show(ex.Message);
            }
        }

        public override void CreateNew()
        {
            try
            {
                FrmEditRole frm = new FrmEditRole(null);
                frm.RequestChangeInstance += new FrmBaseEditForm.OnRequestChangeInstance(LoadData);
                frm.ShowDialog();
            }
            catch (Exception ex)
            {
                //BasicForms.FrmWarning.Show(ex.Message);
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }

        protected override void Edit()
        {
            try
            {
                int[] tmp = gridViewDsDoiTuong.GetSelectedRows();

                if (tmp != null && tmp.Length > 0)
                {
                    int selectedRowIndex = tmp[0];
                    Lists.AccessAuthority role = (Lists.AccessAuthority)gridViewDsDoiTuong.GetRow(selectedRowIndex);
                    if (role != null)
                    {
                        FrmEditRole frm = new FrmEditRole(role);
                        frm.RequestChangeInstance += new FrmBaseEditForm.OnRequestChangeInstance(LoadData);
                        frm.ShowDialog();
                    }
                }
            }
            catch (Exception ex)
            {
                BasicForms.FrmWarning.Show(ex.Message);
            }
        }

        protected override void Delete()
        {
            try
            {
                int[] selectedIndexes = gridViewDsDoiTuong.GetSelectedRows();

                if (selectedIndexes != null && selectedIndexes.Length > 0)
                {
                    int selectedRowIndex = selectedIndexes[0];
                    Lists.AccessAuthority deletedObj = (Lists.AccessAuthority)gridViewDsDoiTuong.GetRow(selectedRowIndex);
                    if (deletedObj != null)
                    {
                        if(DB.Role_Functions.Any(p=>p.RoleID == deletedObj.AccessAuthorityID))
                            throw new Exception("Can not delete this role because this role has function!");
                        if (MessageBox.Show("Are you sure delete " + "'" + deletedObj.AccessAuthorityName + "'" + "?", "Confirm", MessageBoxButtons.YesNo) == DialogResult.No)
                            return;
                        else
                        {
                            DB.AccessAuthorities.DeleteOnSubmit(deletedObj);
                            DB.SubmitChanges();
                            LoadData();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                BasicForms.FrmWarning.Show("Cannot delete this role");
            }
        }
    }
}
